package com.wskj.controller.login;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.system.domain.SysAgreement;
import com.wskj.system.service.ISysAgreementService;
import com.wskj.workbench.vo.app.AppAgreementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 协议管理Controller
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Api(tags = "用户协议和隐私政策")
@AllArgsConstructor
@RestController
@RequestMapping("/api/agreement")
public class ApiAgreementController extends BaseController {

    private final ISysAgreementService iSysAgreementService;

    /**
     * 根据协议类型查询协议
     */
    @ApiOperation("根据协议类型查询协议")
    @GetMapping(value = "/query")
    public AjaxResult<AppAgreementVo> query(
            @ApiParam(value = "协议类型 1:用户协议 2:隐私协议", required = true) @RequestParam(value = "agreementType") String agreementType) {
        LambdaQueryWrapper<SysAgreement> lambdaQueryWrapper = new LambdaQueryWrapper();
        // 协议类型
        lambdaQueryWrapper.eq(SysAgreement::getAgreementType, agreementType);
        // 协议状态
        lambdaQueryWrapper.eq(SysAgreement::getStatus, "0");
        lambdaQueryWrapper.select(SysAgreement::getAgreementId, SysAgreement::getAgreementTitle, SysAgreement::getAgreementContent).last("limit 1");
        SysAgreement sysAgreement = iSysAgreementService.getBaseMapper().selectOne(lambdaQueryWrapper);
        // 数据转换
        AppAgreementVo appAgreementVo = BeanUtil.toBean(sysAgreement, AppAgreementVo.class);
        return AjaxResult.success(appAgreementVo);
    }
}
